﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views.Util
{
    class MaskRectUtil
    {
        /// <summary>
        /// 根据p1和p2决定矩形的大小和位置
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        public static void SetPosition(Rectangle rect, Point p1, Point p2)
        {
            double left = 0;
            double top = 0;
            double width = 0;
            double height = 0;

            if(p1.X <= p2.X)
            {
                left = p1.X;
                width = p2.X - p1.X;
            }
            else
            {
                left = p2.X;
                width = p1.X - p2.X;
            }

            if(p1.Y <= p2.Y)
            {
                top = p1.Y;
                height = p2.Y - p1.Y;
            }
            else
            {
                top = p2.Y;
                height = p1.Y - p2.Y;
            }

            Canvas.SetLeft(rect, left);
            Canvas.SetTop(rect, top);
            rect.Width = width;
            rect.Height = height;
        }

        /// <summary>
        /// 获取矩形的范围，（矩形在canvas中）
        /// </summary>
        /// <param name="rect"></param>
        public static Rect GetRect(Rectangle rect)
        {
            double left = Canvas.GetLeft(rect);
            double top = Canvas.GetTop(rect);
            double width = rect.Width;
            double height = rect.Height;
            return new Rect(left, top, width, height);
        }
        public static Size Calc(Point p1, Point p2)
        {
            double width = 0;
            double height = 0;

            if (p1.X < p2.X)
            {
                width = p2.X - p1.X;
            }
            else
            {
                width = p1.X - p2.X;
            }

            if (p1.Y < p2.Y)
            {
                height = p2.Y - p1.Y;
            }
            else
            {
                height = p1.Y - p2.Y;
            }

            return new Size(width, height);
        }
    }
}
